﻿using BLL.FKTZ;
using Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using YQFK.Models;
using RouteAttribute = System.Web.Http.RouteAttribute;

namespace YQFK.Controllers
{
    /// <summary>
    /// 附件
    /// </summary>
    public class FktzAttachmentController : ApiController
    {
        private readonly FktzAttachmentBLL _attachmentBLL;

        /// <summary>
        /// 附件
        /// </summary>
        public FktzAttachmentController()
        {
            _attachmentBLL = new FktzAttachmentBLL();
        }

        /// <summary>
        /// 下载附件
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>文件</returns>
        [Route("api/Fktz/GetFile")]
        [System.Web.Http.HttpGet]
        public async Task<HttpResponseMessage> Get(string id)
        {
            var model = await _attachmentBLL.GetAsync(id);
            if (model == null) return new HttpResponseMessage(HttpStatusCode.NotFound);

            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
            var stream = new MemoryStream(model.FileStream);
            result.Content = new StreamContent(stream);
            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            result.Content.Headers.ContentDisposition.FileName = model.FileName;
            result.Content.Headers.ContentType = new MediaTypeHeaderValue(model.MediaType);
            //result.Content.Headers.ContentType = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping(fileName));
            result.Content.Headers.ContentLength = stream.Length;
            return result;
        }


    }
}
